import { Injectable, NestMiddleware } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Request, Response, NextFunction } from 'express';
import { Menu } from "src/views/menus/entities/menu.entity";
import { Repository } from "typeorm";


@Injectable() // 装饰器开启依赖注入
export class LoggerMiddleWare implements NestMiddleware {
  // 注入服务
  constructor(
    @InjectRepository(Menu) private menuRepository: Repository<Menu>
  ) {}

  async use(req: Request, res: Response, next: NextFunction) {
    const result = await this.menuRepository.find();
    console.log(result, "测试注入服务");
    console.log(req.originalUrl, req.cookies, req.query, "测试中间件");

    // 没使用res.end或者res.send结束响应，会执行next方法交给下一个中间件处理函数
    // 如果不调用next方法，会卡着无法继续
    next();
  }
}